/**
 * Copyright (C) 2009 Matteo Merli <mm@merlimat.org>
 * 
 * $Id: PxlBoolean.java 15 2009-01-05 14:57:10Z matteo.merli $
 * $URL: http://pxl-lang.googlecode.com/svn/trunk/src/main/java/pxl/types/PxlBoolean.java $
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *         http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package pxl.types;

import java.util.Map;
import java.util.Collection;

/**
 * @author Matteo Merli
 */
public final class PxlBoolean extends PxlObject
{

    public final static PxlBoolean True = new PxlBoolean( true );
    public final static PxlBoolean False = new PxlBoolean( false );
    public final static PxlString sTrue = new PxlString( "True" );
    public final static PxlString sFalse = new PxlString( "False" );

    private final boolean value;

    protected PxlBoolean( boolean value )
    {
        this.value = value;
    }

    @Override
    public String getType()
    {
        return "bool";
    }

    @Override
    public String toString()
    {
        boolean b = this.value;
        return b ? "True" : "False";
    }

    public boolean getValue()
    {
        return value;
    }

    public static PxlBoolean valueOf( boolean value )
    {
        return value ? True : False;
    }
    
    public static PxlBoolean not( PxlBoolean value )
    {
        return value.value ? False : True;
    }

    // // Operators
    @Override
    public PxlString __repr__()
    {
        return value ? sTrue : sFalse;
    }

    @Override
    public PxlString __str__()
    {
        return __repr__();
    }

    @Override
    public PxlObject __mul__( PxlObject other )
    {
        if ( !(other instanceof PxlBoolean) )
        {
            System.out.println( "Type error: " + other.getType() );
            return None.instance();
        }

        PxlBoolean res = new PxlBoolean( (value || ((PxlBoolean) other).value) );
        return res;
    }

    @Override
    public PxlBoolean __bool__()
    {
        return this;
    }

    @Override
    public int hashCode()
    {
        // Hashcodes taken from java.lang.Boolean
        return value ? 1231 : 1237;
    }

    @Override
    public boolean equals( Object other )
    {
        if ( other instanceof PxlBoolean )
        {
            return value == ((PxlBoolean) other).value;
        }
        else if ( other instanceof PxlInteger )
            return (value ? 1 : 0) == ((PxlInteger) other).getValue();
        return false;
    }

    // Attributes

    private static final Map<String, PxlObject> attrs = PxlObject
            .getExportedMethods( PxlBoolean.class );

    @Override
    protected PxlObject getAttribute( String name )
    {
        return attrs.get( name );
    }

    @Override
    protected Collection<String> getAttributeNames()
    {
        return attrs.keySet();
    }

}
